import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import saveDieProcData from "./functions/saveDieProcData";

const {NCModal, NCButton} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class DieProcModal extends Component {
    constructor(props) {
        super(props);
        this.getEditInitInfo = this.getEditInitInfo.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.beSure = this.beSure.bind(this);
        this.onBeforeFormEvent = this.onBeforeFormEvent.bind(this);
        this.onAfterFormEvent = this.onAfterFormEvent.bind(this);
    }

    getEditInitInfo() {
        let meta = this.props.meta.getMeta();
        meta.passaway.items.find(item => item.attrcode === 'isDisablePsn').visible = this.props.isInJob;
        this.props.meta.setMeta(meta);
    }

    beSure() {
        saveDieProcData.call(this);
    }

    closeModal() {
        this.props.onClose && this.props.onClose();
    }

    onBeforeFormEvent(props, moduleId, key, value, index) {
        if (key === 'pk_org') {
            let that = this;
            let meta = this.props.meta.getMeta();
            meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                return {
                    "pk_org": that.props.orgVal.refpk,//主组织
                    "TreeRefActionExt": "nccloud.web.hrtrn.dimissionrds.sqlbuilder.DieAdminOrgRefSqlBuilder"
                }
            };
            this.props.meta.setMeta(meta);
        } else if (key === 'pk_dept') {
            let meta = this.props.meta.getMeta();
            const newOrg = this.props.form.getFormItemsValue('passaway', 'pk_org');
            if (newOrg.value) {
                meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                    return {
                        "pk_org": newOrg.value
                    }
                };
            }
            this.props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterFormEvent(props, moduleId, key, value, oldValue) {
        if (key === 'pk_org') {
            this.props.form.setFormItemsValue('passaway', {
                'pk_dept': {
                    value: null,
                    display: null
                }
            })
        }
    }

    render() {
        const {visible, form} = this.props;
        return (
            <NCModal
                show={visible}
                size="lg"
                onHide={this.closeModal}
                onEntered={this.getEditInitInfo}
                backdrop={'static'}
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {this.props.json['hi6007-000159']/* 国际化处理： 身故处理*/}
                </NCModalHeader>
                <NCModalBody>
                    {
                        form.createForm('passaway', {
                            onBeforeEvent: this.onBeforeFormEvent,
                            onAfterEvent: this.onAfterFormEvent
                        })
                    }
                </NCModalBody>
                <NCModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.beSure}
                    >
                        {this.props.json['hi6007-000174']}{/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton
                        shape="border"
                        onClick={this.closeModal}
                    >
                        {this.props.json['hi6007-000005']/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default DieProcModal;
